package com.anjoyo.xgh.musicshare.activity;

import java.util.ArrayList;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.adapter.DownSongAdapter;
import com.anjoyo.xgh.musicshare.adapter.LocalSongAdapter;
import com.anjoyo.xgh.musicshare.application.SongApplication;
import com.anjoyo.xgh.musicshare.db.MusicDBhelper;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.service.DownloadService;
import com.anjoyo.xgh.musicshare.service.Task;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.Constant.PLAYLIST;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class LocalMusicAct extends Activity {
	ArrayList<Song> songs;
	ListView listview;
	LocalSongAdapter adapter;
	int currentpos = -1;
	Context con;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.localsong_layout);
		con = this;
		listview = (ListView) findViewById(R.id.lv_localsongs);
		Intent intent = getIntent();
		songs = (ArrayList<Song>) intent.getSerializableExtra("songs");
		adapter = new LocalSongAdapter(this, songs);
		listview.setAdapter(adapter);
		initListener();
		SongApplication.getInstance().changNowPlayList(PLAYLIST.LOCAL_MUSIC_LIST);
	}

	private void initListener() {
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				adapter.changeColor(arg2);
				currentpos = arg2;
				Intent intent = new Intent(Constant.ACTION_PLAY);
				intent.putExtra("pos", currentpos);
				sendBroadcast(intent);
			}
		});
		findViewById(R.id.iv_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		findViewById(R.id.iv_music).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent= new Intent(con,PlayAct.class);
				startActivity(intent);
			}
		});

		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				adapter.changeColor(arg2);
				currentpos = arg2;
				new AlertDialog.Builder(con)
						.setSingleChoiceItems(
								new String[] { "删除", "收藏" }, 0,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										if (which == 0) {
											dialog.dismiss();
											adapter.deletsong(currentpos);
											SongApplication.localsongslist.remove(currentpos);
											SongApplication.getInstance().changNowPlayList(PLAYLIST.LOCAL_MUSIC_LIST);
										} else if (which == 1) {
											dialog.dismiss();
											MusicDBhelper dBhelper = new MusicDBhelper(
													con);
											Song song = (Song) adapter
													.getItem(currentpos);
											long i = dBhelper.saveMusic(song,
													true, false);
											if (i != 0) {
												Toast.makeText(con, "收藏成功", 0)
														.show();
											} else {
												Toast.makeText(con, "已收藏过", 0)
														.show();
											}
										}
									}
								}).setNegativeButton("关闭", null).create()
						.show();
				return false;
			}
		});
	}

}
